#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : lei.wang
import logging

logger = logging.getLogger(__file__)


class WeChatPayConfig:
    # 微信支付商户号（直连模式）或服务商商户号（服务商模式，即sp_mchid)
    # MCHID = '1247321601'
    MCHID = '1624411126'

    # 商户证书私钥路径
    PRIVATE_KEY_PATH = '/Users/wanglei/innerDesktop/lgsc/ai_red_envelope/apps/utils/helper/cert_config/apiclient_key.pem'

    # 商户证书序列号
    # CERT_SERIAL_NO = '4EF6FA9778B0B259FF08E2759DCB7ED91FE97D79'
    CERT_SERIAL_NO = '639DBA8837A0B002EBA1FFACC98C2891475A87DF'

    # API v3密钥
    # APIV3_KEY = 'qLNPamwehViUFd2BmCoaLbbWbCD5Tjk4'
    APIV3_KEY = 'yJ3nK52M8uT4S7wG6P9xZbE1D5QvCrfJ'

    # APPID，应用ID或服务商模式下的sp_appid
    # APPID = 'wx92eb903651be99d0'
    APPID = 'wx196f0260e073a7bb'

    # 回调地址
    NOTIFY_URL = 'http://localhost:8000/api/v1/pay-notify'

    # 微信支付平台证书缓存目录
    CERT_DIR = None  # 示例值:'./cert'

    # 接入模式: False=直连商户模式，True=服务商模式
    PARTNER_MODE = False

    # 代理设置
    PROXY = None  # 示例值: {"https": "http://10.10.1.10:1080"}


def init_wechat_pay():

    with open(WeChatPayConfig.PRIVATE_KEY_PATH) as f:
        private_key = f.read()

    from wechatpayv3 import WeChatPay, WeChatPayType

    return WeChatPay(
        wechatpay_type=WeChatPayType.NATIVE,
        mchid=WeChatPayConfig.MCHID,
        private_key=private_key,
        cert_serial_no=WeChatPayConfig.CERT_SERIAL_NO,
        apiv3_key=WeChatPayConfig.APIV3_KEY,
        appid=WeChatPayConfig.APPID,
        notify_url=WeChatPayConfig.NOTIFY_URL,
        cert_dir=WeChatPayConfig.CERT_DIR,
        logger=logger,
        partner_mode=WeChatPayConfig.PARTNER_MODE,
        proxy=WeChatPayConfig.PROXY
    )


class WxPayHelper:

    @staticmethod
    def get_wx_pay_instance():
        return init_wechat_pay()

